#include <iostream>
#include <algorithm>

using namespace std;

typedef long long LL;

const int N = 5e3 + 10;
int a[N + 1][N + 1];
LL s[N + 1][N + 1];
int n, m;
LL ans;

int main()
{
	cin >> n >> m;
	for(int i = 0; i < n; i++)
	{
		int x, y, v; cin >> x >> y >> v;
		a[x + 1][y + 1] = v;
	}
	
	for(int i = 1; i <= N; i++)
	{
		for(int j = 1; j <= N; j++)
		{
			s[i][j] = a[i][j] + s[i - 1][j] + s[i][j - 1] - s[i - 1][j - 1];
		}
	}
	
	for(int i = 1; i + m - 1 <= N; i++)
	{
		for(int j = 1; j + m - 1 <= N; j++)
		{
			LL ret = s[i + m - 1][j + m - 1] - s[i - 1][j + m - 1] - s[i + m - 1][j - 1] + s[i - 1][j - 1];
			
			ans = max(ret, ans);
		}
	}
	
	cout << ans << endl;
	return 0;
} 
